﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace _001_MVVM基础.Model
{
    /// <summary>
    ///ObservableObject  partial + [ObservableProperty] + 小写开头 + 不需要get;set
    ///ObservableValidator比ObservableObject多了校验功能
    /// </summary>
    public partial class Student: ObservableRecipient //ObservableValidator  //ObservableObject
    {
        [ObservableProperty]
        private int id = 10;

        //public Student()
        //{
        //    SetIdCommand = new RelayCommand(SetId);
        //}

        //public ICommand SetIdCommand { get; }
        //private void SetId() => this.Id++;  


        [RelayCommand]
        private void SetId() => this.Id++;


        //面向切面编程
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(Id))]   //使用的不多见，标注了这行后，可以在OnNameChanged调用id,触发Id属性更新
        [NotifyCanExecuteChangedFor(nameof(UpdateNameCommand))]  //Command后缀  不好演示
        //[Required]   //校验信息
        //[NotifyDataErrorInfo]   //校验信息
        //[MaxLength(100)]   //校验信息

        [NotifyPropertyChangedRecipients]  //通知信息更新
        private string? name;

        partial void OnNameChanging(string? value)
        {//调用要小写
            //value = "OnNameChanging" + value;
            name = "OnNameChanging" + value;
        }

        partial void OnNameChanged(string? value)
        {
            //value = value + "OnNameChanged";
            name = value + "OnNameChanged";
            id = 300;
        }

        [RelayCommand]
        private void SetName()
        {//调用要大写
            //name = "张三";
            Name = "张三";
        }


        [RelayCommand]
        private void UpdateName()
        {
            Console.WriteLine("UpdateName被执行");
        }
    }
}
